<?php
require_once "filter.php";
require_once "Connection.php";
session_start();

$conn = new Connection();
$categories = $conn->fetchCategory($_SESSION['LOGIN_INFO']['id']);
$categories = urldecode($categories);
$categories = json_decode($categories, true);


$sub_categories = $conn->fetchSubCategory($_SESSION['LOGIN_INFO']['id']);
$sub_categories = urldecode($sub_categories);
$sub_categories = json_decode($sub_categories, true);
if(empty($categories) || empty($sub_categories)){
	echo json_encode(array('status'=>false,'msg'=>'發生錯誤'));
	exit;
}


$msg = '';

$msg .= '<tbody>';

foreach ($sub_categories as $category_key=>$categories_val) {
	if(!empty($categories_val[0])){
	    foreach ($categories_val as $sub_category_key=>$category) {
	        $msg .= '<tr>';
	        $msg .= '<td class="title" id="feed_title_'.$category_key.'_'.$sub_category_key.'">'.$category['title'].'</td>';
	        $msg .= '<td class="rename" id="rename_button_'.$category_key.'_'.$sub_category_key.'"><input type="button" value="重新命名" onclick="showRenameTitle(\''.$category_key.'\',\''.$sub_category_key.'\')"/></td>';
	        $msg .= '<td id="unsubscribe_'.$category_key.'_'.$sub_category_key.'"><img src="styles/images/icons/unsubscribe.png" style="cursor:pointer;" onclick="deleteSubscription(\''.$category_key.'\',\''.$sub_category_key.'\')"/></td>';
	        //echo '<td>'.$categories[$category_key].'</td>';
			
	        $msg .= '<td><select id="feed_category_'.$category_key.'_'.$sub_category_key.'" onchange="changeCategory(this,\''.$category_key.'\',\''.$sub_category_key.'\')">';
	        foreach ($categories as $cate_key=>$cate_val) {
	            if ($category_key == $cate_key) {
	                $msg .= '<option selected value="'.$cate_key.'">'.$cate_val.'</option>';
	            } else {
	                $msg .= '<option value="'.$cate_key.'">'.$cate_val.'</option>';
	            }
	        
	        }
	        $msg .= '</select><span style="display:none;" id="loading_img_'.$category_key.'_'.$sub_category_key.'"><img src="../styles/images/ui-anim_basic_16x16.gif"/></span></td>';
	
			
	        $msg .= '</tr>';
	        $msg .= '<tr class="feed-row"><td colspan="4">'.$category['url'].'</td></tr>';
	    }
	}
}


$msg .= '</tbody>';

echo json_encode(array('status'=>true,'msg'=>$msg));

?>